package com.dq.service.org.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgPositionQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.org.centre.OrgDeptPositionCentre;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.org.OrgDeptPositionCentreService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/1/6 15:48
 */
@Service
public class OrgDeptPositionCentreServiceImpl extends BaseRepositories implements OrgDeptPositionCentreService {
    @Override
    public Vo list(OrgPositionQuery orgPositionQuery) throws AdminException{
        orgPositionQuery.format();
        Map<String, Object> query = orgPositionQuery.parseMap();
        if(orgPositionQuery.getOrgId() != null){
            query.put("org_id",orgPositionQuery.getOrgId());
            query.remove("orgId");
        }
        if (StringUtils.isNotBlank(orgPositionQuery.getPostName())) {
            query.put("orgPosition_postNameLike", BaseAdminQuery.likeSql(orgPositionQuery.getPostName()));
            query.remove("postName");
        }
        if(orgPositionQuery.getBeginTime() != null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgPositionQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(orgPositionQuery.getEndTime() != null){
            query.put("createTime"+ BaseDaoImpl.lteSqlStr,orgPositionQuery.getEndTime());
            query.remove("endTime");
        }
        List<OrgDeptPositionCentre> list = dao.find(dao.query2Hql(OrgDeptPositionCentre.class, query), query);
        Long count = dao.count(dao.count2Hql(OrgDeptPositionCentre.class,query),query);
        PageHelper.startPage(orgPositionQuery.getPage(),orgPositionQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<OrgDeptPositionCentre.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo add(Long orgPositionId,OrgDeptPositionCentre orgDeptPositionCentre) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser == null){
            throw new AdminException("登陆用户异常");
        }
        SysSection section = shiroService.getDept();
        if(section == null){
            throw new AdminException("门店信息异常");
        }
        OrgPosition orgPosition = dao.get(OrgPosition.class,orgPositionId);
        if(orgPosition == null){
            throw new AdminException("所选岗位不存在");
        }
        orgDeptPositionCentre.setOrg((Org) section);
        orgDeptPositionCentre.setOrgPosition(orgPosition);
        if(orgDeptPositionRepository.findByOrgAndOrgPositionAndDeletedFalse((Org) section, orgPosition).size()>0){
            throw new AdminException("已经存在 无需添加");
        }
        orgDeptPositionCentre.setCreateUser(sysUser);
        orgDeptPositionCentre.setUpdateUser(sysUser);
        orgDeptPositionCentre.setVersion(Constant.VERSION_VALUE);
        orgDeptPositionCentre.setDeleted(false);
        orgDeptPositionCentre.setCreateTime(System.currentTimeMillis());
        orgDeptPositionCentre.setLastModifyTime(System.currentTimeMillis());

        orgDeptPositionRepository.save(orgDeptPositionCentre);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(Long id,Long orgPositionId, OrgDeptPositionCentre orgDeptPositionCentreEdit) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser == null){
            throw new AdminException("登陆用户异常");
        }
        SysSection section = shiroService.getDept();
        if(section == null){
            throw new AdminException("门店信息异常");
        }
        OrgDeptPositionCentre orgDeptPositionCentre = dao.get(OrgDeptPositionCentre.class,id);
        if(orgDeptPositionCentre == null){
            throw new AdminException("请重新选择");
        }
        OrgPosition orgPosition = dao.get(OrgPosition.class,orgPositionId);
        if(orgPosition == null){
            throw new AdminException("所选岗位不存在");
        }
        if(orgDeptPositionCentre.getOrgPosition().getId() != orgPositionId){
            if(orgDeptPositionRepository.findByOrgAndOrgPositionAndDeletedFalse((Org) section,orgPosition).size()>0){
                throw new AdminException("已经存在 无需添加");
            }
        }
        orgDeptPositionCentre.setOrg((Org) section);
        orgDeptPositionCentre.setOrgPosition(orgPosition);

        orgDeptPositionCentre.setUpdateUser(sysUser);
        orgDeptPositionCentre.setLastModifyTime(System.currentTimeMillis());

        orgDeptPositionRepository.save(orgDeptPositionCentre);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id) throws AdminException {
        OrgDeptPositionCentre orgDeptPositionCentre = dao.get(OrgDeptPositionCentre.class,id);
        if(orgDeptPositionCentre == null){
            throw new AdminException("请重新选择");
        }
        orgDeptPositionCentre.setDeleted(true);
        orgDeptPositionRepository.save(orgDeptPositionCentre);
        return new Vo(HttpStatus.OK);
    }
}
